package com.cheyun.netsalev3.viewmodel.orderquanyi

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import com.cheyun.netsalev3.bean.orderquanyi.OrderQuanyiChildData
import com.cheyun.netsalev3.repository.aftersale.MaintenanceDetailsRepository
import com.cheyun.netsalev3.repository.orderquanyi.OrderQuanyiChildRepository
import com.cheyun.netsalev3.repository.orderquanyi.OrderquanyiDetailsRepository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.view.orderquanyi.OrderQuanyiDetailsActivity
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author Michael
 * @date  2018/10/30 - 15:55.
 */
class OrderQuanyiChildViewModel(var repository: HashMap<String, OrderQuanyiChildRepository>) : ViewModel() {
    var pagedlist = HashMap<String, LiveData<PagedList<OrderQuanyiChildData>>>()
    var hashMap = HashMap<String, LiveData<NetworkState>>()
    var totlelist = HashMap<String, LiveData<Int>>()

    var loginData = MySharedPreferences.getLoginData()

    var detailsRepository = OrderquanyiDetailsRepository(0)

    //点击保养列表
    fun onClickBaoyang(view: View, data: OrderQuanyiChildData) {
        var intent = Intent(view.context, OrderQuanyiDetailsActivity::class.java)
        intent.putExtra("id",data.awid)
        view.context.startActivity(intent)
    }


    init {
        for (key in repository.keys) {
            repository[key]?.apply {
                pagedlist[key] = this.loadAndroidData(null)
            }
        }
    }

    init {
        for (item in repository.keys) {
            pagedlist[item]?.apply {
                hashMap[item] = Transformations.switchMap(this) { repository[item]!!.networkState }!!
                totlelist[item] = Transformations.switchMap(this) { repository[item]!!.total }!!
            }
        }
    }

    fun initData(nkey: String) {
        repository[nkey]?.apply {
            this.setEanbleLoad(true)
            this.dataSourceFactory.sourceLiveData.value?.invalidate()
        }
    }

    /**
     * 刷新
     */
    fun refresh(nkey: String,hashMap:HashMap<String, String>? = null) {
        if(hashMap != null) {
            repository[nkey]?.changeMap(hashMap)
        }
        repository[nkey]?.dataSourceFactory?.sourceLiveData?.value?.invalidate()
    }

    fun getAmountName(item:OrderQuanyiChildData):String{
        return item.award_amount.toString()
    }

    fun visibleOfAmount(item:OrderQuanyiChildData):Int{
        if(item.award_amount == "0"){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun visibleOfEndTime(item:OrderQuanyiChildData):Int{
        if(item.award_end_time.isEmpty()){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun visibleOfBtn(item:OrderQuanyiChildData):Int{
        if(item.isshowbtn == 0){
            return View.GONE
        }
        return View.VISIBLE
    }

    fun onClickQuanyi(view: View, data: OrderQuanyiChildData) {
        var moth: () -> Unit = {
            Toast.makeText(view.context, "权益确认成功", Toast.LENGTH_LONG).show()
        }
        var fail: () -> Unit = {

        }
        detailsRepository.queren(data.awid,moth,fail)
    }

}